﻿namespace Shanda.DevFx.Security.Web.Pages
{
    using Shanda.DevFx.Core;
    using Shanda.DevFx.Data.Attributes;
    using Shanda.DevFx.Data.Utils;
    using Shanda.DevFx.Security;
    using Shanda.DevFx.Security.Web;
    using Shanda.DevFx.Utils;
    using System;
    using System.Web.UI.WebControls;

    public class PermissionEditPage : Shanda.DevFx.Security.Web.BasePage
    {
        protected Button btnSubmit;
        [Column("IsAvailable", ColumnType=typeof(bool))]
        protected DropDownList ddlIsAvailable;
        [Column("IsView", ColumnType=typeof(bool))]
        protected DropDownList ddlIsView;
        [Column("ResType")]
        protected DropDownList ddlResType;
        [Column("ParentNo", WriteOnly=true)]
        protected HyperLink hlkParentNo;
        [Column("LayerIndex", ColumnType=typeof(int), WriteOnly=true)]
        protected Label lblLayerIndex;
        protected Label lblMessage;
        private bool newItem;
        private string parentNo;
        private string permissionNo;
        protected RegularExpressionValidator revDispIndex;
        protected RequiredFieldValidator rfvDispIndex;
        protected RequiredFieldValidator rfvPermissioNo;
        protected RequiredFieldValidator rfvTitle;
        [Column("Description")]
        protected TextBox txtDescription;
        [Column("DispIndex", ColumnType=typeof(int))]
        protected TextBox txtDispIndex;
        protected TextBox txtPermissionNo;
        [Column("Res1")]
        protected TextBox txtRes1;
        [Column("Res2")]
        protected TextBox txtRes2;
        [Column("Title")]
        protected TextBox txtTitle;
        protected ValidationSummary vsPermission;

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            if (!base.get_IsValid())
            {
                this.lblMessage.set_Text("请正确填写表单");
            }
            else
            {
                Permission item;
                if (this.newItem)
                {
                    if (Checker.CheckEmptyString("ParentNo", this.parentNo, false))
                    {
                        item = Securitor.PermissionManager.CreateItem();
                    }
                    else
                    {
                        item = Securitor.PermissionManager.CreateItem(this.hlkParentNo.get_Text());
                    }
                    item.PermissionNo = this.txtPermissionNo.get_Text();
                }
                else
                {
                    item = Securitor.PermissionManager.GetItem(this.permissionNo);
                }
                DataTransfer.TransWebFormDataToEntityProperty(this, item);
                IAOPResult result = item.Save();
                if (result.IsFailed)
                {
                    this.lblMessage.set_Text(result.ResultDescription);
                }
                else
                {
                    this.lblMessage.set_Text("成功保存");
                }
            }
        }

        private void InitializeComponent()
        {
            this.btnSubmit.add_Click(new EventHandler(this.btnSubmit_Click));
            base.add_Load(new EventHandler(this.Page_Load));
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
        }

        private void Page_Load(object sender, EventArgs e)
        {
            this.permissionNo = base.get_Request().get_Params()["PermissionNo"];
            this.parentNo = base.get_Request().get_Params()["ParentNo"];
            this.newItem = Checker.CheckEmptyString("PermissionNo", this.permissionNo, false);
            if (!base.get_IsPostBack())
            {
                this.ddlResType.set_DataTextField("Title");
                this.ddlResType.set_DataValueField("ResType");
                this.ddlResType.set_DataSource(Securitor.ResManager.ListResTypes());
                this.ddlResType.DataBind();
                if (!this.newItem)
                {
                    Permission item = Securitor.PermissionManager.GetItem(this.permissionNo);
                    if (item == null)
                    {
                        WebHelper.ShowMessage("没找到此权限，或者您没有权限操作", "history.back()", true, true);
                    }
                    this.txtPermissionNo.set_Text(item.PermissionNo);
                    this.txtPermissionNo.set_ReadOnly(true);
                    DataTransfer.TransEntityPropertyToWebFormData(item, this);
                }
                else
                {
                    this.hlkParentNo.set_Text(this.parentNo);
                }
                this.hlkParentNo.set_NavigateUrl(string.Format("permissionEdit.aspx?PermissionNo={0}", this.hlkParentNo.get_Text()));
            }
        }
    }
}

